package cn.stylefeng.guns.modular.business.frequency.transportlevel.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.transportlevel.entity.TransportLevel;
import cn.stylefeng.guns.modular.business.frequency.transportlevel.mapper.TransportLevelMapper;
import cn.stylefeng.guns.modular.business.frequency.transportlevel.model.param.TransportLevelParam;
import cn.stylefeng.guns.modular.business.frequency.transportlevel.service.TransportLevelService;
import cn.stylefeng.guns.modular.business.frequency.transportlevel.enums.TransportLevelExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 运单运输车辆详情 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:15
 */
@Service
public class TransportLevelServiceImpl extends ServiceImpl<TransportLevelMapper, TransportLevel> implements TransportLevelService {

    @Override
    public PageResult<TransportLevel> page(TransportLevelParam transportLevelParam) {

        // 构造条件
        LambdaQueryWrapper<TransportLevel> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<TransportLevel> list(TransportLevelParam transportLevelParam) {

        // 构造条件
        LambdaQueryWrapper<TransportLevel> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(TransportLevelParam transportLevelParam) {

        // 将dto转为实体
        TransportLevel transportLevel = new TransportLevel();
        BeanUtil.copyProperties(transportLevelParam, transportLevel);

        this.save(transportLevel);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(TransportLevelParam transportLevelParam) {
        this.removeById(transportLevelParam.getId());
    }

    @Override
    public void edit(TransportLevelParam transportLevelParam) {

        // 根据id查询实体
        TransportLevel transportLevel = this.queryTransportLevel(transportLevelParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(transportLevelParam, transportLevel);

        this.updateById(transportLevel);
    }

    @Override
    public TransportLevel detail(TransportLevelParam transportLevelParam) {
        return this.queryTransportLevel(transportLevelParam);
    }



    /**
     * 获取运单运输车辆详情
     *
     * @author yujie
     * @since 2023/05/09 17:15
     */
    private TransportLevel queryTransportLevel(TransportLevelParam transportLevelParam) {
        TransportLevel transportLevel = this.getById(transportLevelParam.getId());
        if (ObjectUtil.isEmpty(transportLevel)) {
            throw new ServiceException(TransportLevelExceptionEnum.NOT_EXISTED);
        }
        return transportLevel;
    }
}